package product;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class HolyReason extends JFrame {

	HashMap<String, MemberData> holy; // 사용해쉬맵
	MemberHoly my; // 

	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel label;
	private JLabel label_1;
	private JScrollPane scrollPane;
	private JLabel label_3;
	private JScrollPane scrollPane_1;
	private JLabel label_4;
	private JScrollPane scrollPane_2;
	private JLabel lblNewLabel;
	JTextArea accident;
	JTextArea reason;
	JTextArea result;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	public HolyReason(HashMap<String, MemberData> holy, MemberHoly mh) { // 매개변수

		this.holy = holy;
		this.my = mh;

		setTitle("사유서 보기");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 478, 367);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.SOUTH);
		contentPane.add(getPanel_1(), BorderLayout.CENTER);

		String[] hyrn = my.Reason.split("%");   // 창을띄웟을때 사고 사유 처리에 불러들이는 값
		accident.setText(hyrn[0]);
		reason.setText(hyrn[1]);
		result.setText(hyrn[2]);


	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setPreferredSize(new Dimension(10, 30));
			panel.add(getBtnNewButton());
			panel.add(getBtnNewButton_1());
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			GridBagLayout gbl_panel_1 = new GridBagLayout();
			gbl_panel_1.columnWidths = new int[]{0, 0, 0};
			gbl_panel_1.rowHeights = new int[]{0, 0, 0, 0, 0};
			gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
			gbl_panel_1.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
			panel_1.setLayout(gbl_panel_1);
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.fill = GridBagConstraints.BOTH;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 0;
			gbc_label.gridy = 0;
			panel_1.add(getLabel(), gbc_label);
			GridBagConstraints gbc_label_1 = new GridBagConstraints();
			gbc_label_1.insets = new Insets(0, 0, 5, 0);
			gbc_label_1.gridx = 1;
			gbc_label_1.gridy = 0;
			panel_1.add(getLabel_1(), gbc_label_1);
			GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
			gbc_lblNewLabel.fill = GridBagConstraints.VERTICAL;
			gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel.gridx = 0;
			gbc_lblNewLabel.gridy = 1;
			panel_1.add(getLblNewLabel(), gbc_lblNewLabel);
			GridBagConstraints gbc_scrollPane = new GridBagConstraints();
			gbc_scrollPane.fill = GridBagConstraints.BOTH;
			gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
			gbc_scrollPane.gridx = 1;
			gbc_scrollPane.gridy = 1;
			panel_1.add(getScrollPane(), gbc_scrollPane);
			GridBagConstraints gbc_label_3 = new GridBagConstraints();
			gbc_label_3.fill = GridBagConstraints.BOTH;
			gbc_label_3.insets = new Insets(0, 0, 5, 5);
			gbc_label_3.gridx = 0;
			gbc_label_3.gridy = 2;
			panel_1.add(getLabel_3(), gbc_label_3);
			GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
			gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
			gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
			gbc_scrollPane_1.gridx = 1;
			gbc_scrollPane_1.gridy = 2;
			panel_1.add(getScrollPane_1(), gbc_scrollPane_1);
			GridBagConstraints gbc_label_4 = new GridBagConstraints();
			gbc_label_4.fill = GridBagConstraints.BOTH;
			gbc_label_4.insets = new Insets(0, 0, 0, 5);
			gbc_label_4.gridx = 0;
			gbc_label_4.gridy = 3;
			panel_1.add(getLabel_4(), gbc_label_4);
			GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
			gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
			gbc_scrollPane_2.gridx = 1;
			gbc_scrollPane_2.gridy = 3;
			panel_1.add(getScrollPane_2(), gbc_scrollPane_2);
		}
		return panel_1;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("");
			label.setPreferredSize(new Dimension(80, 0));
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("내용");
		}
		return label_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(80, 100));
			scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			scrollPane.setViewportView(getAccident());
		}
		return scrollPane;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("사유");
			label_3.setPreferredSize(new Dimension(0, 60));
			label_3.setHorizontalAlignment(SwingConstants.CENTER);
			label_3.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		}
		return label_3;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			scrollPane_1.setViewportView(getTextArea_3());
		}
		return scrollPane_1;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("처리");
			label_4.setPreferredSize(new Dimension(0, 60));
			label_4.setHorizontalAlignment(SwingConstants.CENTER);
			label_4.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		}
		return label_4;
	}
	private JScrollPane getScrollPane_2() {
		if (scrollPane_2 == null) {
			scrollPane_2 = new JScrollPane();
			scrollPane_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			scrollPane_2.setViewportView(getTextArea_1_1());
		}
		return scrollPane_2;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사고");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			lblNewLabel.setPreferredSize(new Dimension(80, 0));
		}
		return lblNewLabel;
	}
	private JTextArea getAccident() {
		if (accident == null) {
			accident = new JTextArea();
			accident.setEnabled(false);
		}
		return accident;
	}
	private JTextArea getTextArea_3() {
		if (reason == null) {
			reason = new JTextArea();
			reason.setEnabled(false);
		}
		return reason;
	}
	private JTextArea getTextArea_1_1() {
		if (result == null) {
			result = new JTextArea();
			result.setEnabled(false);
		}
		return result;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("승인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {


					try{
						int row = -1;
						row = my.ResultTable.getSelectedRow();
						String hk = (String)my.model.getValueAt(my.ResultTable.getSelectedRow(), 0); //발생년월일
						String hkk = (String)my.model.getValueAt(row, 1); // 사원코드
						if(row != -1){
							int  yn =JOptionPane.showConfirmDialog(null, "승인 하시겠습니까? ");
							if(yn == JOptionPane.YES_OPTION){
								MemberData data = new MemberData(hkk,hk,"4", "승인", 
										accident.getText()+"%"
												+ reason.getText()+"%"
												+ result.getText());
								holy.put("["+hkk+"]"+hk, data);
								//파일로 실시간 백업
//								my.oos = new ObjectOutputStream(new FileOutputStream("/MemberManage/Holy"));
//								my.oos.writeObject(my.pm.holy);
//								my.oos.close();
								// 결과 갱신
								my.model.setValueAt((String)"4",row, 2);
								my.model.setValueAt("승인", row, 3);

								JOptionPane.showMessageDialog(null, "승인하셨습니다. <유급처리> ");
							}
							else{
								JOptionPane.showMessageDialog(null, "작업이 취소 되었습니다. ");
							}
						}

					}catch(Exception ee){
						JOptionPane.showMessageDialog(null, "입력형식이 잘못되었습니다. ");
					}





				}
			});
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("승인취소");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					try{
						int row = -1;
						row = my.ResultTable.getSelectedRow();
						String hk = (String)my.model.getValueAt(my.ResultTable.getSelectedRow(), 0); //날
						String hkk = (String)my.model.getValueAt(row, 1); // 사원코드
						if(row != -1){
							int  yn =JOptionPane.showConfirmDialog(null, "승인을 취소 하시겠습니까? ");
							if(yn == JOptionPane.YES_OPTION){
								MemberData data = new MemberData(hkk,hk,"0", "사유서결격", 
										accident.getText()+"%"
												+ reason.getText()+"%"
												+ result.getText());
								holy.put("["+hkk+"]"+hk, data);

//								my.oos = new ObjectOutputStream(new FileOutputStream("/MemberManage/Holy"));
//								my.oos.writeObject(my.pm.holy);
//								my.oos.close();

								my.model.setValueAt((String)"0",row, 2);
								my.model.setValueAt("사유서결격", row, 3);

								JOptionPane.showMessageDialog(null, "승인을 거절하셨습니다. <무급처리> ");
							}
							else{
								JOptionPane.showMessageDialog(null, "작업이 취소 되었습니다. ");
							}
						}

					}catch(Exception ee){
						JOptionPane.showMessageDialog(null, "입력형식이 잘못되었습니다. ");
					}



				}
			});
		}
		return btnNewButton_1;
	}
}
